<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <title>Title</title>
</head>
<body>
<table align="center" border="1" th:title="用户管理">
<tr>
    <th>token</th>
    <th>sessionId</th>
    <th>操作</th>
</tr>
<tr th:each="u : ${list}">
    <td th:text="${u.token}"></td>
    <td th:text="${u.sessionId}"></td>
    <td><button onclick="clickSend(this)">精准推送</button></td>
</tr>
</table>
</body>
<!-- 这两个js一定要导入 -->
<script src="http://cdn.bootcss.com/stomp.js/2.3.3/stomp.min.js"></script>
<script src="https://cdn.bootcss.com/sockjs-client/1.1.4/sockjs.min.js"></script>
<script>
    // var date = new Date().getTime();
    // var socket = new SockJS('http://localhost:8080/portfolio-stomp?token='+date);
    var socket = new SockJS('/websocket-stomp');

    /**
     * 建立成功的回调函数
     */
    socket.onopen = function() {
        console.log('open');
    };

    /**
     * 服务器有消息返回的回调函数
     */
    socket.onmessage = function(e) {
        console.log('message', e.data);
    };

    /**
     * websocket链接关闭的回调函数
     */
    socket.onclose = function() {
        console.log('close');
    };

    var stompClient = Stomp.over(socket);

    function clickSend(elm) {
        var token = elm.parentElement.parentElement.childNodes[1].innerText;
        var obj = {'toUser':token, 'msg' : "精准推送"};
        stompClient.send("/app/foo.handle1", {}, JSON.stringify(obj));
    }
</script>
</html>